package com.bh.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 代码
 *
 * @author shuangluxing@163.com
 * @date 2023/03/01
 */
@Component
public class RedisCodeUtil {


    @Autowired
    private RedisTemplate redisTemplate;

    public static final String ORDER_CODE = "D00000";//订单
    public static final String SHIPMENT_CODE = "S00000";//运单
    public static final String DISPATCH_CODE = "P00000";//派车单
    public static final String VEHICLE_PLAN_CODE = "V00000";//车辆任务
    public static final String PRESCO_CODE = "Y00000";//预录单
    public static final String SEND_CODE = "T00000";//预录单
    public static final String BACK_CODE = "B00000";//预录单

    public String getOrderCode() {
        return this.getDateCode("D",ORDER_CODE);
    }

    public String getShipmentCode() {
        return this.getDateCode("S",SHIPMENT_CODE);
    }

    public String getSendCode() {
        return this.getDateCode("T",SEND_CODE);
    }

    public String getBackCode() {
        return this.getDateCode("B",BACK_CODE);
    }

    public String getDispatchCode() {
        return this.getDateCode("P",DISPATCH_CODE);
    }

    public String getVehiclePlanCode() {
        return this.getDateCode("V",VEHICLE_PLAN_CODE);
    }

    public String getPrescoCode() {
        return this.getDateCode("Y",PRESCO_CODE);
    }


    /**
     * 依赖Redis的原子性，设置数据中的自动递增，有效期到12点
     *
     * @param key
     */
    public String getDateCode(String prefix, String key) {
        StringBuilder orderNo = new StringBuilder();
        String format = new SimpleDateFormat("yyMM").format(new Date());
        orderNo.append(prefix).append(format);
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, Objects.requireNonNull(redisTemplate.getConnectionFactory()));
        //当缓存中没有时，默认时0
        long increment = entityIdCounter.getAndIncrement();
        if (increment == 0) {
            //当数据为0的时候，设置有效期,10秒后过期
            //entityIdCounter.expire(3600, TimeUnit.SECONDS);
            //设置到什么时间过期, 默认为一个月
            entityIdCounter.expireAt(new Date(getFirstDayOfNext()));
        }
        //当日返回的id最低六位
        increment = increment + 1;
        orderNo.append(new StringBuilder(String.format("%06d", increment)));
        return orderNo.toString();
    }

    /**
     * 获取次日凌晨的时间戳
     *
     * @return
     */
    public long getNextDay() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        //今天凌晨
        return c.getTimeInMillis() / 1000 * (24 * 60 * 60);
    }

    /***
     * 获取某一时间 所在 月份 下个月的第一天
     *  比如 ： 2020-01-20
     *     结果为：2020-02-01 00:00:00
     * */
    public static long getFirstDayOfNext() {
        Calendar nextMonthFirst = Calendar.getInstance();
        nextMonthFirst.setTime(new Date());
        nextMonthFirst.set(Calendar.DAY_OF_MONTH, 1);
        nextMonthFirst.add(Calendar.MONTH, 1);
        //今天凌晨
        return nextMonthFirst.getTimeInMillis() / 1000 * (24 * 60 * 60);
    }
}
